//this program calculates a utility bill
//written by Judy Mullins
//cs101 

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int get_usage(int beg_reading, int end_reading);
double calculate_bill(int beg_reading, int end_reading, int& usage);
void print(ostream& outfile, string acct_num, int end_reading, int beg_reading,
           int kwh, double bill);

int main()
{
	string acct_num;
	int beg_reading, end_reading;
	int kwh;
	double bill;

	ifstream infile("utility.dat");
	ofstream outfile("utility.out");

	if(!infile||!outfile)
	{
		cout <<"a file did not open";
		exit(1);
	}


	infile >> acct_num;
	while(acct_num != "End")
	{
		infile >> beg_reading >> end_reading;
		bill = calculate_bill(beg_reading,end_reading,kwh);
		print(outfile, acct_num, beg_reading,end_reading, kwh, bill);
		infile >> acct_num;		
	}
	return 0;
}


int determine_usage(int start, int stop)
{
	int usage;

	usage = stop - start;
	if (usage < 0)
		usage= 100000 + usage;		//adjust for roll-over
	return usage;
}

double calculate_bill(int beg_reading,int end_reading, int& kwh)
{
	kwh = determine_usage(beg_reading,end_reading);

	if(kwh < 100)
		return 10.0;
	else if (kwh <= 250)
		return 10.0 + 0.25 * (kwh-100);
	else	return 20.0 + 0.15 * (kwh - 250);

}

void print(ostream& outfile, string acct_num, int end_reading, int beg_reading, int kwh, 
           double bill)
{
 		outfile << acct_num << '\t' << end_reading << endl
    	        	<< '\t' << '\t' << "    " << beg_reading << endl
			<< '\t' << '\t' << "------" << endl
			<< '\t' << '\t' << "    " << kwh << " kwh" << endl
			<< "Balance due:  " << '$' << bill
			<< endl << endl;
}

